package com.cjm.mybatis.test2;

import com.cjm.mybatis.MybatisUtil.MybatisUtils;
import com.cjm.mybatis.bean.User;
import com.cjm.mybatis.test3.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.util.List;

/**
 * Created by jiaming.chen1 on 2015/7/7.
 */
public class Test2 {

    @Test
    public void addUser(){
        SqlSessionFactory factory = MybatisUtils.getFactory();

        SqlSession session = factory.openSession();

        String statement = "com.cjm.mybatis.test2.userMapper.addUser";

        int insert = session.insert(statement, new User(12,"niaho", 11));
        System.out.println(insert);
        session.commit();
    }


    @Test
    public void Update(){
        SqlSessionFactory factory = MybatisUtils.getFactory();

        SqlSession session = factory.openSession(true);

        String statement = "com.cjm.mybatis.test2.userMapper.updateUser";

        session.update(statement, new User(7, "wwww", 111));

    }

    @Test
    public void Delete(){
        SqlSessionFactory factory = MybatisUtils.getFactory();

        SqlSession session = factory.openSession(true);

        String statement = "com.cjm.mybatis.test2.userMapper.deleteUser";

        session.delete(statement, 6);

    }

    @Test
    public void getAll(){
        SqlSessionFactory factory = MybatisUtils.getFactory();

        SqlSession session = factory.openSession(true);

        String statement = "com.cjm.mybatis.test2.userMapper.getAllUsers";

        List<User> users = session.selectList(statement);

        System.out.println(users);

    }

    @Test
    public void getUser(){
        SqlSessionFactory factory = MybatisUtils.getFactory();

        SqlSession session = factory.openSession(true);

        UserMapper mapper = session.getMapper(UserMapper.class);

        int add = mapper.addUser(new User(-1, "123", 12));
        System.out.println(add);
    }
}
